package org.csu.onlineedubackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.onlineedubackend.entity.Course;
import org.csu.onlineedubackend.mapper.CourseMapper;
import org.csu.onlineedubackend.service.CourseFilterService;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.result.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class CourseFilterServiceImpl implements CourseFilterService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public CommonResponse<List<Course>> getFilteredCourses(String category, String level, String sort) {
        return getFilteredCourses(category, level, sort, null);
    }
    
    @Override
    public CommonResponse<List<Course>> getFilteredCourses(String category, String level, String sort, String searchQuery) {
        try {
            QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
            
            // 只选择必要的字段，提高查询效率
            queryWrapper.select("id", "teacher_id", "title", "category", "level", 
                               "cover_url", "intro_md", "price", "preview_percent", 
                               "status", "created_at", "updated_at", "subscriber_count");
            
            // 添加分类筛选
            if (category != null && !category.isEmpty()) {
                queryWrapper.eq("category", category);
            }
            
            // 添加级别筛选
            if (level != null && !level.isEmpty()) {
                queryWrapper.eq("level", level);
            }
            
            // 添加搜索条件
            if (searchQuery != null && !searchQuery.isEmpty()) {
                queryWrapper.and(wrapper -> wrapper
                    .like("title", searchQuery)
                    .or()
                    .like("intro_md", searchQuery)
                );
            }
            
            // 添加排序
            if (sort != null) {
                switch (sort) {
                    case "new":
                        queryWrapper.orderByDesc("created_at");
                        break;
                    case "hot":
                        queryWrapper.orderByDesc("subscriber_count");
                        break;
                    default: // recommend - 默认按订阅数和创建时间的组合排序
                        queryWrapper.orderByDesc("subscriber_count");
                        queryWrapper.orderByDesc("created_at");
                        break;
                }
            } else {
                // 默认排序
                queryWrapper.orderByDesc("subscriber_count");
                queryWrapper.orderByDesc("created_at");
            }
            
            // 只选择状态为上架的课程
            queryWrapper.eq("status", 1);
            
            // 使用列表查询，不使用分页，直接获取所有结果
            List<Course> courses = courseMapper.selectList(queryWrapper);
            return CommonResponse.createForSuccess(ResponseCode.SUCCESS.getCode(), "获取课程列表成功", courses);
        } catch (Exception e) {
            return CommonResponse.createForError(ResponseCode.ERROR.getCode(), "获取课程列表失败：" + e.getMessage());
        }
    }
}